package com.lib.common.utils

import java.math.BigDecimal
import java.math.RoundingMode
import java.text.DecimalFormat

/**
 * 数值工具类
 *
 * @author keanbin
 */
class NumberUtil {

    companion object {

        /**
         * 对 Double 数值的小数位进行格式
         * @param num 数值
         * @param bits 小数位数
         * @return
         */
        fun doubleFormat(num: Double, bits: Int, roundingMode: Int = BigDecimal.ROUND_DOWN): Double {
            val b = BigDecimal(num)
            return b.setScale(bits, roundingMode).toDouble()
        }

        /**
         * Double 数值 转为字符串
         * @param num 数值
         * @param bits 小数位数
         * @return
         */
        fun doubleToString(num: Double, bits: Int):String {
            if (bits == 0) {
                return num.toInt().toString()
            }

            val sb = StringBuilder()
            for (i in 0 until bits) {
                sb.append("0")
            }

            val df = DecimalFormat("0.$sb")
            df.roundingMode = RoundingMode.FLOOR
            var str =  df.format(num) ?: return num.toString()
            if (str[0] == '.') {
                str = "0$str"
            }

            return str
        }

        /**
         * 去除小数点后面的0
         */
        fun doubleDelZero(num: Double):String {
            val decimalFormat = DecimalFormat("###################.###########")
            return decimalFormat.format(num)
        }

        /**
         * 去除小数点后面的0
         * @param maxBits 小数位数
         */
        fun doubleDelZero(num: Double, maxBits: Int):String {
            if (maxBits <= 0) return num.toInt().toString()

            val sb = StringBuilder()
            for (i in 0 until maxBits) {
                sb.append("#")
            }

            val decimalFormat = DecimalFormat("###################.$sb")
            decimalFormat.roundingMode = RoundingMode.DOWN
            return decimalFormat.format(num)
        }

        /**
         * 按精度去掉多余0
         */
        fun doubleLimitString(num:Double, maxBits: Int):String{
            if(maxBits < 0) return num.toInt().toString()
            if(maxBits == 0) return doubleDelZero(num,maxBits)
            val intStr = num.toInt().toString()
            val splitArray = num.toString().split(".")
            var smallPartStr = ""
            when(splitArray.size){
                2->{
                    val smallNum = splitArray[1]
                    if(smallNum.length > maxBits){
                        smallPartStr = smallNum.substring(0, maxBits)
                    }else{
                        smallPartStr = smallNum
                    }
                }
            }

            val sb = StringBuilder()
            sb.append(intStr)
            if(smallPartStr.isNotEmpty()){
                sb.append(".")
                sb.append(smallPartStr)
            }
            return sb.toString()
        }


        /**
         * 按精度去掉多余0
         */
        fun numStrLimitString(num:String, maxBits: Int):String{
            if(maxBits <= 0) return num
            val splitArray = num.split(".")
            var smallPartStr = ""
            var intStr = num
            when(splitArray.size){
                2->{
                    intStr = splitArray[0]
                    val smallNum = splitArray[1]
                    if(smallNum.length > maxBits){
                        smallPartStr = smallNum.substring(0, maxBits)
                    }else{
                        smallPartStr = smallNum
                    }
                }
            }

            val sb = StringBuilder()
            sb.append(intStr)
            if(smallPartStr.isNotEmpty()){
                sb.append(".")
                sb.append(smallPartStr)
            }
            return sb.toString()
        }

        /**
         * 获取最小值（最小精度）
         *
         * @param bit 小数的位数
         */
        fun getMinValue(bit: Int):Double {
            if (bit <= 0) return 1.0

            val str = StringBuilder()
            str.append("0.")
            for (i in 0 until bit - 1) {
                str.append("0")
            }
            str.append("1")

            try {
                return str.toString().toDouble()
            } catch (e: Exception) {

            }

            return 0.1
        }
    }

}